package com.hechi.demo.service;

import com.hechi.demo.entity.Doctor;
import com.hechi.demo.dto.ResultResponse;
import com.hechi.demo.exception.TijianException;
import com.hechi.demo.exception.TijianExceptionEnum;
import com.hechi.demo.mapper.DoctorMapper;
import com.hechi.demo.util.WebUtils;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class DoctorServiceImpl {
    @Autowired
    DoctorMapper doctorMapper;
    @Autowired
    WebUtils webUtils;

    public static void main(String[] args) {
        String password="123456";
        // 123456 888888  999999 iloveyou football
        String salt="#*)$@)$U)$@U$)@[[[{{";
       String result=DigestUtils.md5Hex(password+salt);

        System.out.println("result:"+result); //7cec85c75537840dad40251576e5b757
    }

    /**
     * 登录的业务方法
     */
    public ResultResponse login(Doctor doctor){
        ResultResponse resultResponse=null;
        //算法：如何给resultResponse赋值？赋什么值？  医生账号错误  密码错误  登录成功
        Doctor result=doctorMapper.selectByDocCode(doctor.getDocCode());
        if(result==null){
            throw new TijianException(TijianExceptionEnum.DOCCODE_NOT_REGISTER.getEnCode(),TijianExceptionEnum.DOCCODE_NOT_REGISTER.getEnMessage());
        }

        if(!webUtils.md5(doctor.getPassword()).equals(result.getPassword())){
           throw new TijianException(TijianExceptionEnum.PASSWORD_IS_WRONG.getEnCode(),TijianExceptionEnum.PASSWORD_IS_WRONG.getEnMessage());
        }

        result.setPassword("**************");

        resultResponse=new ResultResponse(ResultResponse.SUCCESS_CODE,ResultResponse.SUCCESS_DESC,result);

        return resultResponse;
    }
}
